{% extends "../../main.html" %}

{% block title %}{{ _("Game Levels") }}{% end %}

{% block header %}
<script src="/static/js/pages/admin/view/game_levels.js"></script>
{% end %}

{% block modals %}
{% from tornado.options import options %}
{% from models.GameLevel import GameLevel %}
    <!-- Edit Level -->
    <div id="edit-game-level-modal" class="modal hide fade" style="display: none; ">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h3>
                <i class="fa fa-pencil"></i>
                {{ _("Edit Level") }}
            </h3>
        </div>
        <div class="modal-body">
            <form class="form-horizontal" id="edit-game-level-form" method="post" action="/admin/edit/game_level">
                {% raw xsrf_form_html() %}
                <input id="game_level-uuid" name="uuid" type="hidden" />
                <input id="game_level-last_level" name="last_level" type="hidden" />
                <div class="control-group">
                    <label class="control-label" for="game_level-number">{{ _("Game Level") }}</label>
                    <div class="controls">
                        <input required id="game_level-number" name="number" pattern="[0-9]+" placeholder="{{ _('Level Number') }}" type="number" min=0 />
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="game_level-name">{{ _("Level Name") }}</label>
                    <div class="controls">
                        <input id="game_level-name" name="name" placeholder="{{ _('Level Name') }}" maxlength="32" type="text" />
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="game_level-reward">{{ _("Completion Reward") }}</label>
                    <div class="controls">
                        <input id="game_level-reward" name="reward" pattern="[0-9]+" placeholder="{{ _('Reward') }}" type="number" min=0 />
                    </div>
                </div>
                <div id="gametype" class="control-group">
                    <label class="control-label" for="game_level-type">{{ _("Unlock Level") }}</label>
                    <div class="controls">
                        <select id="game_level-type" name="type">
                            <option value="none">{{ _("No Lock") }}</option>
                            <option value="locked">{{ _("Locked") }}</option>
                            <option value="buyout">{{ _("Purchase Unlock") }}</option>
                            <option value="progress">{{ _("Game Progress") }}</option>
                            <option value="points">{{ _("Game Points") }}</option>
                            <option value="hidden">{{ _("Managed") }}</option>
                        </select>
                    </div>
                </div>
                <div id="buyoutcost" class="control-group">
                    <label id="buyoutlabel" class="control-label" for="game_level-buyout">{{ _("Unlock Cost") }}</label>
                    <div class="controls">
                        <input id="game_level-buyout" name="buyout" pattern="[0-9]+" placeholder="" type="number" min=0 />
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="game_level-description">{{ _("Description") }}</label>
                    <div class="controls">
                        <textarea id="game_level-description" name="description" placeholder="{{_('Optional')}}" type="text"></textarea>
                    </div>
                </div>
            </form>
        </div>
        <div class="modal-footer">
            <a href="#" class="btn" data-dismiss="modal">{{ _("Close") }}</a>
            <button id="edit-game-level-submit" class="btn btn-primary" type="button">
                <i class="fa fa-save"></i>
                {{ _("Save") }}
            </button>
        </div>
    </div>
    <!-- Level Access -->
    <div id="edit-access-modal" class="modal hide fade" style="display: none; ">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h3>
                <i class="fa fa-pencil"></i>
                {{ _("Edit Access") }}
            </h3>
        </div>
        <div class="modal-body">
            <form class="form-horizontal" id="edit-access-form" method="post" action="/admin/edit/level_access">
                {% raw xsrf_form_html() %}
                <input id="edit-access-uuid" name="uuid" type="hidden" />
                <div>
                    <table width="100%">
                        <tbody>
                            <tr><td>Available</td><td>Access</td></tr>
                            <tr>
                                <td style="padding: 5px 0;">
                                    <select id="availableList" name="availableList" multiple="multiple"
                                        style="width: 200px; height: 170px; margin: 0px 2px 0px 3px;">
                                    </select>
                                </td>
                                <td style="padding: 5px 0;">
                                    <select id="accessList" name="accessList" multiple="multiple"
                                        style="width: 200px; height: 170px; margin: 0px 2px 0px 3px;">
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <button id="addToLevel" class="btn btn-mini" type="button">
                                        &nbsp;&nbsp;&nbsp;{{ _("Add") }}&nbsp;&nbsp;&nbsp;
                                    </button>
                                </td>
                                <td>
                                    <button id="removeFromLevel" class="btn btn-mini" type="button">
                                        &nbsp;{{ _("Remove") }}&nbsp;
                                    </button>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </form>
        </div>
        <div class="modal-footer">
            <a href="#" class="btn" data-dismiss="modal">{{ _("Close") }}</a>
            <button id="edit-access-submit" class="btn btn-primary" type="button">
                <i class="fa fa-save"></i>
                {{ _("Save") }}
            </button>
        </div>
    </div>
    <!-- Delete Level -->
    <div id="delete-game-level-modal" class="modal hide fade" style="display: none; ">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h3><i class="fa fa-times-circle-o"></i> {{ _("Delete Game Level") }}</h3>
        </div>
        <div class="modal-body">
        <p>
            {{ _("Are you sure you want to delete this game level?") }} <br /> <br />
            <strong>{{ _("WARNING") }}:</strong>
            {{ _("This will also delete boxes and flags associated with this level.") }} <br />
        </p>
        <form id="delete-game-level-form" method="post" action="/admin/delete/game_level">
            {% raw xsrf_form_html() %}
            <input id="delete-game-level-uuid" name="uuid" type="hidden" value="" />
        </form>
        </div>
        <div class="modal-footer">
            <a href="#" class="btn" data-dismiss="modal">{{ _("Close") }}</a>
            <button id="delete-game-level-submit" type="button" class="btn btn-danger">
                <i class="fa fa-times-circle-o"></i>
                {{ _("Delete") }}
            </button>
        </div>
    </div>
    <!-- Switch Level -->
    <div id="switch-level-modal" class="modal hide fade" style="display: none; ">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h3>
                <i class="fa fa-arrows-v"></i>
                {{ _("Switch Level") }}
            </h3>
        </div>
        <div class="modal-body">
            <form id="switch-level-form" class="form-horizontal" id="switch-level" method="post" action="/admin/edit/box_level">
                {% raw xsrf_form_html() %}
                <input id="box-uuid" name="box_uuid" type="hidden" />
                <div class="control-group">
                    <label class="control-label" for="game-level-uuid">{{ _("Current Level") }}</label>
                    <div class="controls">
                        <select id="game-level-uuid" name="level_uuid">
                            {% for level in GameLevel.all() %}
                                <option value="{{ level.uuid }}">{{ level.number }}</option>
                            {% end %}
                        </select>
                    </div>
                </div>
            </form>
        </div>
        <div class="modal-footer">
            <a href="#" class="btn" data-dismiss="modal">{{ _("Close") }}</a>
            <button id="switch-level-submit" class="btn btn-primary" type="button">
                <i class="fa fa-save"></i>
                {{ _("Save") }}
            </button>
        </div>
    </div>
{% end %}

{% block content %}
{% from models.GameLevel import GameLevel %}
    <div class="container">
        {% if errors != None and len(errors) != 0 %}
            {% for error in errors %}
                <div class="alert alert-error">
                    <a class="close" data-dismiss="alert" href="#">&times;</a>
                    <h4 class="alert-heading">{{ _("ERROR") }}</h4>
                    {{ error }}
                </div>
            {% end %}
        {% end %}
        <h1>
            <i class="fa fa-level-up"></i>
            {{ _("Game Levels") }}
        </h1>
        <br />
        {% for index, level in enumerate(GameLevel.all()) %}
            <div class="well">
                <h2 class="levelheader">
                    {{ level.name }}&nbsp;
                </h2>
                {% if level.description %}<h4 class="levelheader">({{ level.description }})</h4>{% end %}
                <a id="delete-game-level-button{{ index }}" style="float: right; margin-top: 5px; margin-right: 10px;" class="btn btn-small btn-danger" data-toggle="modal" href="#delete-game-level-modal" data-uuid="{{ level.uuid }}">
                    <i class="fa fa-times-circle-o"></i>
                    {{ _("Delete Game Level") }}
                </a>
                <a id="edit-game-level-button{{ index }}" style="float: right; margin-top: 5px; margin-right: 10px;" class="btn btn-small" data-toggle="modal" href="#edit-game-level-modal" data-uuid="{{ level.uuid }}">
                    <i class="fa fa-pencil"></i>
                    {{ _("Edit Game Level") }}
                </a>
                <button type="button" id="edit-access-button{{ index }}" style="float: right; margin-top: 5px; margin-right: 10px;" class="btn btn-small" data-toggle="modal" href="#edit-access-modal" data-uuid="{{ level.uuid }}"{% if level.type == "none" %} disabled{%end%}>
                    <i class="fa fa-pencil"></i>
                    {{ _("Edit Access") }}
                </button>
                <div style="padding-left: 3px; margin-bottom: -10px;">
                {% if level._name  and level._name != level.name %}<strong>{{ _("Level") }} #{{ level.number }}</strong>&nbsp;&nbsp;&ndash;&nbsp;{% end %}
                    {% if level.type == "hidden" %}
                        <i class="fa fa-lock"></i> {{ _("Managed") }}
                    {% elif level.type == "locked" %}
                        <i class="fa fa-lock"></i> {{ _("Locked") }}
                    {% elif level.type == "none" or level.buyout == 0 %}
                        <i class="fa fa-unlock-alt"></i> {{ _("No Lock") }}
                    {% elif level.type == "buyout" %}
                        <i class="fa fa-lock"></i> {{ _("Unlocks for") }} {% if options.banking %}{{ _("$") }}{% end %}{{ level.buyout }}{% if not options.banking %} {{ _("points") }}{% end %}
                    {% elif level.type == "points" %}
                        <i class="fa fa-lock"></i> {{ _("Unlocks at") }} {% if options.banking %}{{ _("$") }}{% end %}{{ level.buyout }}{% if not options.banking %} {{ _("points") }}{% end %}
                    {% else %}
                        {% set last = GameLevel.last_level(level.id) %}
                        <i class="fa fa-lock"></i> {{ _("Unlocks after") }} {{ level.buyout }}% {{ _("completion of Level") }} {% if last %}{{ last.number }}{% end %}
                    {% end %}
                    <span style="float: right; padding-right: 15px;"><strong>{{ _("Reward") }}: {{ level._reward }}</strong></span>
                </div>
                <hr />
                {% if len(level.boxes) != 0 %}
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>{{ _("Box Name") }}</th>
                                <th class="shortcolumn">{{ _("Number of Flags") }}</th>
                                <th><!-- Switch Level --></th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for index, box in enumerate(level.boxes) %}
                                <tr>
                                    <td class="shortcolumn">{{ index + 1 }}</td>
                                    <td>{{ box.name }}</td>
                                    <td class="shortcolumn" style="text-align: center;">{{ len(box.flags) }}</td>
                                    <td class="shortcolumn" style="margin-right: 10px;">
                                          <a id="switch-level-button{{ index }}" class="btn btn-mini" data-toggle="modal" href="#switch-level-modal" data-level-uuid="{{ level.uuid }}" data-box-uuid="{{ box.uuid }}">
                                            <i class="fa fa-arrows-v"></i>
                                            {{ _("Switch Level") }}
                                          </a>
                                    </td>
                                </tr>
                            {% end %}
                        </tbody>
                    </table>
                {% else %}
                    <strong>{{ _("No boxes on this level yet") }}, <a href="/admin/create/box">{{ _("add some") }}</a></strong>
                {% end %}
            </div>
        {% end %}
    </div>
{% end %}